<template>
    <el-dialog width="40%" title="上传图片" center
               :visible="uploadImagesVisible" @close="closeModel">
        <div style="width: 100%;display: flex;justify-content: center">
            <el-upload
                class="avatar-uploader"
                :action="url"
                :data="{
                    itemNum: scope.itemNum,
                    _st: token
                }"
                :show-file-list="false"
                style="width: 120px;height: 120px;"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload">
                <i style="width: 100%;height: 60px;line-height: 60px" class="el-icon-plus"></i>
                <div class="ant-upload-text">Upload</div>
            </el-upload>
        </div>

    </el-dialog>
</template>

<script>
import config from '../../axios/config';
import {materialImageUrl} from "../../axios/url";
import UserHelper from "../../helpers/UserHelper";
const Cookie = require('js-cookie');

    export default {
        name: "upload-image",
        props: {
            scope: {},
            uploadImagesVisible: null,
        },
        data() {
            return {
                url: config.host + materialImageUrl,
                token: UserHelper.getToken() && Cookie.get('token')
            };
        },
        methods: {
            handleAvatarSuccess(res, fiel) {
                console.log(fiel);
                if(res && res.resultMess) {
                    this.$emit('hiddeUploadImages')
                }
            },
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 2;

                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                return isLt2M;
            },
            closeModel() {
                this.$emit('hiddeUploadImages')
            }
        }
    }
</script>

<style scoped>

</style>
